package com.tzstcl.sys.user.ctrl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tzstcl.base.ctrl.BaseCtrl;
import com.tzstcl.base.model.AjaxResult;
import com.tzstcl.framework.constants.AppConstants;
import com.tzstcl.sys.user.model.SysMenu;
import com.tzstcl.sys.user.service.impl.SysMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 权限菜单控制器
 *
 * @author haocj
 */
@Controller
@RequestMapping(value = "/admin/sysMenu")
public class SysMenuCtrl extends BaseCtrl {

    @Autowired
    private SysMenuService menuService;


    @RequestMapping(value = "/getById")
    @ResponseBody
    public SysMenu getById(SysMenu sysMenu) {
        SysMenu menu = menuService.get(sysMenu);
        return menu;
    }

    /**
     * 菜单列表
     *
     * @param sysMenu
     * @return
     */
    @RequestMapping(value = "/info")
    @RequiresPermissions("sys:menu:info")
    @ResponseBody
    public List<SysMenu> list(SysMenu sysMenu) {
        return menuService.findListAll(sysMenu);
    }

    /**
     * 新增
     *
     * @param sysMenu
     * @return
     */
    @RequestMapping(value = "/save")
    @RequiresPermissions("sys:menu:save")
    @ResponseBody
    public AjaxResult save(SysMenu sysMenu) {
        menuService.save(sysMenu);
        return success();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions("sys:menu:delete")
    @ResponseBody
    public AjaxResult delete(@RequestParam("id") String[] ids) {
        menuService.delete(ids);
        return success();
    }


    /**
     * 菜单数据
     *
     * @return
     */
    @RequestMapping(value = "/zTreeNodes")
    @ResponseBody
    public AjaxResult getZTreeNodes() {

        JSONArray array = new JSONArray();
        JSONObject object = null;
        List<SysMenu> list = menuService.findListAll(new SysMenu());
        for (SysMenu menu : list) {
            object = new JSONObject();
            object.put("id", ""+menu.getId());
            object.put("pId",""+menu.getParentId());
            object.put("name", menu.getName());
            array.add(object);
        }
        return success(AppConstants.SUCCESS_MSG, array);
    }

}
